<?php

namespace app\admin\controller;

use taobao\AliOss;
use think\Db;
use app\enumerations\SceneStatusEnum;
use app\enumerations\FigureItemTypeEnum;

class Scene extends Common {

    /**
     * 列表
     * @return mixed
     */
    public function index() {

        $list = $this->sceneService->getSceneList();

        $this->assign('list', $list);
        return $this->fetch();

    }

    /**
     * 添加场景
     * @return string
     */
    public function addScene() {

        $post = $this->request->post();
        $tempFile = $_FILES['file']['tmp_name'];

        $fileName = md5(uniqid(mt_rand(), true)).".".strtolower(pathinfo($_FILES['file']['name'])["extension"]);

        $info = AliOss::uploadFile($tempFile,$fileName);

        $url = $info['info']['url'];
        $start = strrpos($url,"/");
        $imgName = substr($url, $start);
        $iconPath = config('oss.Cname').$imgName;

        $maxSort = $this->sceneService->getMaxSort();

        $data['title'] = $post['title'];
        $data['remark'] = $post['remark'];
        $data['icon_path'] = $iconPath;
        $data['sort'] = isNullOrEmpty($maxSort) ? 5 : $maxSort['sort']+5;

        $this->sceneService->saveByData($data);

        $this->success('上传成功');

    }

    /**
     * 修改场景
     */
    public function editScene() {

        $post = $this->request->post();

        $scene = $this->sceneService->findById($post['id']);

        if (isNullOrEmpty($scene)) {
            $this->error('场景不存在');
        }

        $tempFile = $_FILES['file']['tmp_name'];

        $fileName = md5(uniqid(mt_rand(), true)).".".strtolower(pathinfo($_FILES['file']['name'])["extension"]);

        $info = AliOss::uploadFile($tempFile,$fileName);

        $url = $info['info']['url'];
        $start = strrpos($url,"/");
        $imgName = substr($url, $start);
        $iconPath = config('oss.Cname').$imgName;

        $sceneData['title'] = $post['title'];
        $sceneData['remark'] = $post['remark'];
        $sceneData['icon_path'] = $iconPath;

        $this->sceneService->updateByIdAndData($post['id'], $sceneData);

        $this->success('修改成功');

    }

    /**
     * 场景信息
     */
    public function sceneInfo() {

        $sceneId = $this->request->post("scene_id");

        $scene = $this->sceneService->findById($sceneId);

        if (isNullOrEmpty($scene)) {
            $this->error('场景不存在');
        }

        $this->success('', '', $scene);
    }

    /**
     * 排序操作
     */
    public function order() {

        $param = input('post.');

        $updateList = array();

        foreach ($param as $id => $sort) {
            $info["id"] = $id;
            $info["sort"] = $sort;

            array_push($updateList, $info);
        }

        $this->sceneService->updateByList($updateList);

        $this->success("排序更新成功");

    }

    /**
     * 软删除
     */
    public function delete() {

        $id = $this->request->param('id');

        $data['status'] = SceneStatusEnum::getEnumValue(SceneStatusEnum::ABANDON_CODE);

        $this->sceneService->updateByIdAndData($id, $data);

        $this->success('删除成功');

    }

    /**
     * 更新小店图片素材接口缓存
     */
    public function updateCache() {

        // 场景图片列表
        $sceneList = $this->sceneService->getSceneListForApi();

        foreach ($sceneList as &$val) {
            $val['item_json'] = json_decode($val['item_json'], true);
        }

        // 门面图片列表
        $facadeList = $this->facadeService->getFacadeListForApi();

        // 元素图片列表
        $elementList = $this->elementService->getElementListForApi();

        // 人物列表
        $figureList = $this->figureService->getFigureListForApi();

        foreach ($figureList as $key=>$val) {

            // 头部
            $itemList['head'] = $this->figureItemService
                ->getFigureItemListByFigureIdAndIconType($val['id'],
                    FigureItemTypeEnum::getEnumValue(FigureItemTypeEnum::HEAD_CODE));

            //上半身
            $itemList['aboveTheWaist'] = $this->figureItemService
                ->getFigureItemListByFigureIdAndIconType($val['id'],
                    FigureItemTypeEnum::getEnumValue(FigureItemTypeEnum::ABOVETHEWAIST_CODE));
            // 下半身
            $itemList['lowerBody'] = $this->figureItemService
                ->getFigureItemListByFigureIdAndIconType($val['id'],
                    FigureItemTypeEnum::getEnumValue(FigureItemTypeEnum::LOWERBODY_CODE));
            // 人物其他
            $itemList['other'] = $this->figureItemService
                ->getFigureItemListByFigureIdAndIconType($val['id'],
                    FigureItemTypeEnum::getEnumValue(FigureItemTypeEnum::OTHER_CODE));

            $figureList[$key]['item_list'] = $itemList;
        }

        $xdPictureMaterial['scene_list'] = $sceneList;
        $xdPictureMaterial['facade_list'] = $facadeList;
        $xdPictureMaterial['element_list'] = $elementList;
        $xdPictureMaterial['figure_list'] = $figureList;

        $result = setXdPictureMaterial(json_encode($xdPictureMaterial));

        if (!$result) {
            $this->error("更新失败，请稍后再试");
        }

        $this->success('更新成功');
    }

}